﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.IO;
using System.Drawing;
public partial class administrator_uc_admin_e_brands : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {           
            if (!string.IsNullOrEmpty(Request.QueryString["itemid"]))
            {                
                LoadDataInfo(int.Parse(Request.QueryString["itemid"].ToString().Trim()));                
            }
        }
    }   
    private void LoadDataInfo(int ItemID)
    {
        DataTable dt = new BLL.CmsBLL().Brands_ViewItemByItemID(ItemID);
        if (dt.Rows.Count > 0)
        {
            hdItemID.Value = dt.Rows[0]["BrandID"] + "";
            txtBrandName.Text = dt.Rows[0]["BrandName"] + "";
            txtUrl.Text = dt.Rows[0]["Url"] + "";                  
            txtSortIndex.Text = dt.Rows[0]["SortIndex"] + "";
            rdActive.SelectedValue = dt.Rows[0]["Active"] + "";
            LoadImage(dt.Rows[0]["Image"].ToString());
        }
        else
            lblMessage.Text = "Load data failt!";
    }
    private void LoadImage(string FileName)
    {
        try
        {
            ltViewImage.Text = "<img src=\"../images/" + FileName + "\" />\n";
        }
        catch (Exception ex)
        {
            ltViewImage.Text = "Load image error! " + ex.Message;
        }
        //if (FileType.ToLower().Equals("image"))
        //    ltViewBanner.Text = "<img src=\"../images/" + FileName + "\" />\n";
        //else
        //    ltViewBanner.Text = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'\n" +
        //                    " style='height: 62px' width='447px'>\n" +
        //                    " <param name='movie'\n" +
        //                    " value='../images/" + FileName + "' />\n" +
        //                    " <param name='quality' value='high' />\n" +
        //                    " <embed height='62 pluginspage='http://www.macromedia.com/go/getflashplayer' quality='high'\n" +
        //                    " src='../images/" + FileName + "' type='application/x-shockwave-flash' width='447'>\n" +
        //                    " </embed>\n" +
        //                    " </object>";
    }
    protected void lbtEditItem_Click(object sender, EventArgs e)
    {
        if (FileUploadImage.HasFile)
        {
            string strFileName = FileUploadImage.FileName;
            try
            {
                Stream fStream = FileUploadImage.FileContent;
                strFileName = FileUploadImage.FileName.Replace(' ', '_');
                strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);
                strFileName = CommonBLL.GetFilename(strFileName);

                string filepathsmallimage = Server.MapPath("" + DAL.EnvInfo.PathUpload + "smallimages//") + strFileName;
                string filepathlargeimage = Server.MapPath("" + DAL.EnvInfo.PathUpload + "images//") + strFileName;

                // Validate the uploaded file
                // 1. The uploaded file should not be a blank file
                if (FileUploadImage.FileBytes.Length == 0)
                {
                    lblMessage.Text = "File bạn chọn bị lỗi! dung lượng file = " + FileUploadImage.FileBytes.Length.ToString() + " bytes";
                    return;
                }
                // 2. The uploaded file should be having one of the allowed extensions
                string ext = System.IO.Path.GetExtension(FileUploadImage.FileName).TrimStart(".".ToCharArray()).ToLower();
                if ((ext != "jpeg") && (ext != "jpg") && (ext != "png") && (ext != "gif") && (ext != "bmp"))
                {
                    lblMessage.Text = "Định dạng file bạn chọn không hợp lệ (File hợp lệ dạng file ảnh)";
                    return;
                }

                // Validation successful
                // Load the image into Bitmap Object

                //Bitmap uploadedImage = new Bitmap(FileUploadBrands.FileContent);

                // Set the maximum width and height here.
                // You can make this versatile by getting these values from
                // QueryString or textboxes
                //int maxWidth = 170;
                //int maxHeight = 50;

                // Resize the image
                //Bitmap resizedImage = CommonBLL.GetScaledPicture(uploadedImage, maxWidth, maxHeight);

                //Save the image
                // String virtualPath = "~/temp/" + System.Guid.NewGuid().ToString() + "." + ext;
                //String tempFileName = Server.MapPath(virtualPath);
                // resizedImage.Save(filepathsmallimage, uploadedImage.RawFormat);
                FileUploadImage.SaveAs(filepathlargeimage);
                new CmsBLL().Brands_UpDateItem(int.Parse(hdItemID.Value), txtBrandName.Text, strFileName, txtUrl.Text + "", int.Parse("0" + txtSortIndex.Text), int.Parse("0" + rdActive.SelectedValue));
                DataTable dt = new CmsBLL().Brands_ViewItemByItemID(int.Parse("0" + hdItemID.Value));
                if (dt.Rows.Count > 0)
                    LoadImage(dt.Rows[0]["Image"].ToString());
                lblMessage.Text = "Cập nhật thành công!";
            }
            catch (Exception ex)
            {
                lblMessage.Text = "ERROR! " + ex.Message.ToString();
            }
        }
        else
        {
            try
            {
                new CmsBLL().Brands_UpDateItem(int.Parse(hdItemID.Value), txtBrandName.Text, "", txtUrl.Text + "", int.Parse("0" + txtSortIndex.Text), int.Parse("0" + rdActive.SelectedValue));
                lblMessage.Text = "Cập nhật thành công!";

            }
            catch (Exception ex)
            {
                lblMessage.Text = "ERROR! " + ex.Message.ToString();
            }
        }        
    }   
}